package com.cspg.design.ui.search;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cspg.design.CSPGApplication;
import com.cspg.design.R;
import com.cspg.design.adapter.Style;
import com.cspg.design.bean.deviceBean.PointDeviceBean;
import com.cspg.design.bean.deviceBean.PolylineDeviceBean;
import com.cspg.design.dto.ProjectDto;
import com.cspg.design.presenter.ProjectPresenter;
import com.cspg.design.ui.gtasks.GTasksActivity;
import com.cspg.design.ui.map.MapActivity;
import com.cspg.design.ui.project.FlowActivity;
import com.cspg.design.ui.project.ProjectInfoActivity;
import com.cspg.design.ui.project.ProjectParameterActivity;
import com.cspg.design.utils.ActionBarUtil;
import com.cspg.design.utils.StatusBarUtil;
import com.cspg.design.utils.Utils;
import com.cspg.design.widget.AlertListDialog;
import com.cspg.design.widget.Loading;
import com.cspg.design.widget.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

public class SearchActivity extends AppCompatActivity {

    private ListView lvSearchResult;

    private ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
    private SimpleAdapter adapter ;
    private List<ProjectDto> projectBeanList = new ArrayList<>();

    private TextView noData ;
    private ProjectPresenter projectPresenter;
    int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);
        ActionBarUtil.setActionBar(this, R.string.search);
        lvSearchResult = findViewById(R.id.lv_search_result);
        noData = findViewById(R.id.tv_no_data);
        Intent intent = getIntent();
        type = intent.getIntExtra("type",0);
        EditText searTxt = findViewById(R.id.et_search_txt);
        adapter = new SimpleAdapter(SearchActivity.this, dataList, R.layout.listview_gtasks_project_item,
                new String[]{"title", "name", "status"},
                new int[]{R.id.item_gtasks_project_title, R.id.item_gtasks_project_name, R.id.item_gtasks_project_status}
        );
        lvSearchResult.setAdapter(adapter);
        projectPresenter = new ProjectPresenter(this,lvSearchResult,adapter,dataList);
        if (type == 1)
        {
            searTxt.setFocusable(true);
            searTxt.setFocusableInTouchMode(true);
            searTxt.requestFocus();
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
        else
        {
            ActionBarUtil.setActionBar(this, R.string.home_design);
            search(type,searTxt.getText().toString());
        }
        searTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭软键盘
                    Utils.closeBoard(SearchActivity.this);
                    search(type,searTxt.getText().toString());
                    return true;
                }
                return false;
            }
        });
        lvSearchResult.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                projectBeanList = projectPresenter.getProjectDto();
                if (projectBeanList != null && projectBeanList.size() > position)
                {
                    ProjectDto projectDto = projectBeanList.get(position);
                    choiceScheme(projectDto);
                }
                else
                {
                    ToastUtils.toast(SearchActivity.this,"项目信息获取出错。。。");
                }
            }
        });
    }

    private void search(int type,String txt)
    {
        if (type == 1)
        {
            projectPresenter.getProject(txt);
        }
        else if (type == 2)
        {
            projectPresenter.getProjectHistory(txt);
        }
    }

    private void refreshListView()
    {

        dataList.clear();
        for (int i = 0; i < projectBeanList.size(); i++) {
            HashMap<String, String> hashMap = new HashMap<String, String>();
            hashMap.put("title", projectBeanList.get(i).getProjectName());
            hashMap.put("name", projectBeanList.get(i).getProjectName());
            dataList.add(hashMap);
        }
        adapter.notifyDataSetChanged();
    }

    //方案选择对话框
    private void choiceScheme(final ProjectDto projectDto) {
        if (type == 1)
        {
            //到审核界面
            new AlertListDialog.ListBuilder(this).setDividerColorRes(R.color.color_divider)
                    .setDividerHeightRes(R.dimen.dp1)
                    .setItems(new String[]{"评审","工程信息","流程信息","设备地图"}, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent;
                            switch (which)
                            {
                                case 0:
                                    intent = new Intent(SearchActivity.this, ProjectParameterActivity.class);
                                    intent.putExtra("projectId", projectDto.getProjectID());
                                    startActivity(intent);
                                    break;
                                case 1:
                                    intent = new Intent(SearchActivity.this, ProjectInfoActivity.class);
                                    intent.putExtra("projectId", projectDto.getProjectID());
                                    startActivity(intent);
                                    break;
                                case 2:
                                    intent = new Intent(SearchActivity.this, FlowActivity.class);
                                    intent.putExtra("projectId", projectDto.getProjectID());
                                    startActivity(intent);
                                    break;
                                case 3:
                                    intent = new Intent(SearchActivity.this, MapActivity.class);
                                    intent.putExtra("projectId", projectDto.getProjectID());
                                    startActivity(intent);
                                    break;
                            }
                        }
                    }).create().show();
        }
        else if (type == 2)
        {
            //到审核界面
            new AlertListDialog.ListBuilder(this).setDividerColorRes(R.color.color_divider)
                    .setDividerHeightRes(R.dimen.dp1)
                    .setItems(new String[]{"工程信息","流程信息","设备地图"}, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent;
                            switch (which)
                            {
                                case 0:
                                    intent = new Intent(SearchActivity.this, ProjectInfoActivity.class);
                                    intent.putExtra("projectId", projectDto.getProjectID());
                                    startActivity(intent);
                                    break;
                                case 1:
                                    intent = new Intent(SearchActivity.this, FlowActivity.class);
                                    intent.putExtra("projectId", projectDto.getProjectID());
                                    startActivity(intent);
                                    break;
                                case 2:
                                    intent = new Intent(SearchActivity.this, MapActivity.class);
                                    intent.putExtra("projectId", projectDto.getProjectID());
                                    startActivity(intent);
                                    break;
                            }
                        }
                    }).create().show();
        }


    }
}